package com.tjec.project.projectLeaderWorkbeanch.service.impl;

import java.util.List;
import java.util.UUID;

import com.tjec.common.utils.DateUtils;
import com.tjec.common.utils.SecurityUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tjec.project.projectLeaderWorkbeanch.mapper.BmsLeaderAgencyHistoryMapper;
import com.tjec.project.projectLeaderWorkbeanch.domain.BmsLeaderAgencyHistory;
import com.tjec.project.projectLeaderWorkbeanch.domain.BmsLeaderMember;
import com.tjec.project.projectLeaderWorkbeanch.service.IBmsLeaderAgencyHistoryService;
import com.tjec.project.projectLeaderWorkbeanch.service.IBmsLeaderMemberService;

/**
 * 项目负责人委托历史变更记录 Service业务层处理
 * 
 * @author pyf
 * @date 2021-04-22
 */
@Service
public class BmsLeaderAgencyHistoryServiceImpl implements IBmsLeaderAgencyHistoryService 
{
    @Autowired
    private BmsLeaderAgencyHistoryMapper bmsLeaderAgencyHistoryMapper;
    
    @Autowired
    private IBmsLeaderMemberService bmsLeaderMemberService;

    /**
     * 查询项目负责人委托历史变更记录 
     * 
     * @param guid 项目负责人委托历史变更记录 ID
     * @return 项目负责人委托历史变更记录 
     */
    @Override
    public BmsLeaderAgencyHistory selectBmsLeaderAgencyHistoryByGuid(String guid)
    {
        return bmsLeaderAgencyHistoryMapper.selectBmsLeaderAgencyHistoryByGuid(guid);
    }

    /**
     * 查询项目负责人委托历史变更记录 列表
     * 
     * @param bmsLeaderAgencyHistory 项目负责人委托历史变更记录 
     * @return 项目负责人委托历史变更记录 
     */
    @Override
    public List<BmsLeaderAgencyHistory> selectBmsLeaderAgencyHistoryList(BmsLeaderAgencyHistory bmsLeaderAgencyHistory)
    {
        return bmsLeaderAgencyHistoryMapper.selectBmsLeaderAgencyHistoryList(bmsLeaderAgencyHistory);
    }

    /**
     * 新增项目负责人委托历史变更记录 
     * 
     * @param bmsLeaderAgencyHistory 项目负责人委托历史变更记录 
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBmsLeaderAgencyHistory(BmsLeaderAgencyHistory bmsLeaderAgencyHistory)
    {
        bmsLeaderAgencyHistory.setCreateTime(DateUtils.getNowDate());
        bmsLeaderAgencyHistory.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserId());
        bmsLeaderAgencyHistory.setUpdateTime(DateUtils.getNowDate());
        bmsLeaderAgencyHistory.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId());
        
        BmsLeaderMember bmsLeaderMember = new  BmsLeaderMember();
        bmsLeaderMember.setGuid(bmsLeaderAgencyHistory.getGuid());
        bmsLeaderMember.setAgencyFlag("1");
        bmsLeaderMemberService.updateAgencyFlag(bmsLeaderMember);
        return bmsLeaderAgencyHistoryMapper.insertBmsLeaderAgencyHistory(bmsLeaderAgencyHistory);
    }
    
    @Transactional
    @Override
    public int insertCancleBmsLeaderAgencyHistory(BmsLeaderAgencyHistory bmsLeaderAgencyHistory)
    {
        bmsLeaderAgencyHistory.setCreateTime(DateUtils.getNowDate());
        bmsLeaderAgencyHistory.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserId());
        bmsLeaderAgencyHistory.setUpdateTime(DateUtils.getNowDate());
        bmsLeaderAgencyHistory.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId());
        
        BmsLeaderMember bmsLeaderMember = new  BmsLeaderMember();
        bmsLeaderMember.setGuid(bmsLeaderAgencyHistory.getGuid());
        bmsLeaderMember.setAgencyFlag("0");
        bmsLeaderMemberService.updateAgencyFlag(bmsLeaderMember);
        return bmsLeaderAgencyHistoryMapper.insertBmsLeaderAgencyHistory(bmsLeaderAgencyHistory);
    }
    

    /**
     * 修改项目负责人委托历史变更记录 
     * 
     * @param bmsLeaderAgencyHistory 项目负责人委托历史变更记录 
     * @return 结果
     */
    @Override
    public int updateBmsLeaderAgencyHistory(BmsLeaderAgencyHistory bmsLeaderAgencyHistory)
    {
        bmsLeaderAgencyHistory.setUpdateTime(DateUtils.getNowDate());
        return bmsLeaderAgencyHistoryMapper.updateBmsLeaderAgencyHistory(bmsLeaderAgencyHistory);
    }

    /**
     * 批量删除项目负责人委托历史变更记录 
     * 
     * @param guids 需要删除的项目负责人委托历史变更记录 ID
     * @return 结果
     */
    @Override
    public int deleteBmsLeaderAgencyHistoryByGuids(String[] guids)
    {
        return bmsLeaderAgencyHistoryMapper.deleteBmsLeaderAgencyHistoryByGuids(guids);
    }

    /**
     * 删除项目负责人委托历史变更记录 信息
     * 
     * @param guid 项目负责人委托历史变更记录 ID
     * @return 结果
     */
    @Override
    public int deleteBmsLeaderAgencyHistoryByGuid(String guid)
    {
        return bmsLeaderAgencyHistoryMapper.deleteBmsLeaderAgencyHistoryByGuid(guid);
    }
}
